import axios from 'axios';

export default {
    name: 'CoachPerformance',
    data() {
        return {
            coachInfo: [],
            currentCoachId: null,
            currentWage: 0,
            newWage: 0,
        };
    },
    created: function() {
        this.fetchCoaches();
    },
    methods: {


        //显示教练信息
        async fetchCoaches() {
            try {
                const url = "http://localhost:8081/coach/list";
                const response = await axios.get(url);

                this.coachInfo = response.data.data.map(coach => {
                    // 检查 coach.image 是否为空，并设置默认图片（请使用适当的默认图片地址替换）
                    const imagePath = coach.image ? coach.image.replace(/^D:.*image/, '/image') : '/path/to/default/image.png';
                    console.log(imagePath);
                    return {
                        ...coach,
                        image: imagePath,
                    };
                });
            } catch (error) {
                console.error("Fetching coaches error:", error);
            }
        },
        async showWageDialog(id) {
            this.currentCoachId = id;
            try {
                const response = await axios.get(`http://localhost:8081/coach/wage/${id}`);
                this.currentWage = response.data?.data?.wage || 0;
            } catch (error) {
                console.error("获取教练工资失败：", error);
            }
        },
        //加工资
        async addWage(id, wage) {
            try {
                await axios.post(`http://localhost:8081/coach/addWage/${id}`, {}, { params: { wage }});
                // 工资发放成功的提示
                this.$message.success("发放成功！");
                this.newWage = 0;
            } catch (error) {
                console.error("工资发放失败：", error);
                // 工资发放失败的提示
            }
        },

    },
};